package com.sxm.yiwei.pinche;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.Order;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.utils.Calculator;
import com.sxm.yiwei.views.AmountView;

import java.util.Calendar;
import java.util.Date;

public class HostTicketRefundActivity extends BaseActivity {


    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_refund_style;
    private TextView tv_refund_desciption;
    private ImageView iv_icon;
    private TextView tv_phone;
    private TextView tv_host_words;
    private TextView tv_start_pos;
    private TextView tv_end_pos;
    private TextView tv_seats;
    private TextView tv_total_price;
    private TextView tv_order_code;
    private TextView tv_depart_time;
    private LinearLayout ll_loss_paid;
    private TextView tv_compensation_money;
    private TextView tv_return_back;
    private CheckBox checkBox;
    private TextView tv_pinche_rule;
    private TextView tv_pinche_count_rule;
    private Button btn_refund;
    private LinearLayout ll_loss_not_paid;
    private Order order;
    private int go_type;

    @Override
    public void setContentView() {
        order = (Order) getIntent().getSerializableExtra("order");
        go_type = getIntent().getIntExtra("go_type",-1);
        setContentView(R.layout.activity_host_ticket_refund);
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_refund_style = (TextView)findViewById(R.id.tv_refund_style);
        tv_refund_desciption = (TextView)findViewById(R.id.tv_refund_desciption);
        iv_icon = (ImageView)findViewById(R.id.iv_icon);
        tv_phone = (TextView)findViewById(R.id.tv_phone);
        tv_host_words = (TextView)findViewById(R.id.tv_host_words);
        tv_start_pos = (TextView)findViewById(R.id.tv_start_pos);
        tv_end_pos = (TextView)findViewById(R.id.tv_end_pos);
        tv_seats = (TextView)findViewById(R.id.tv_transfer_fee);
        tv_total_price = (TextView)findViewById(R.id.tv_total_price);
        tv_order_code = (TextView)findViewById(R.id.tv_order_code);
        tv_depart_time = (TextView)findViewById(R.id.tv_depart_time);
        ll_loss_paid = (LinearLayout)findViewById(R.id.ll_loss_paid);
        ll_loss_not_paid = (LinearLayout)findViewById(R.id.ll_loss_not_paid);
        tv_compensation_money = (TextView)findViewById(R.id.tv_compensation_money);
        tv_return_back = (TextView)findViewById(R.id.tv_return_back);
        checkBox = (CheckBox)findViewById(R.id.checkbox);
        tv_pinche_rule = (TextView)findViewById(R.id.tv_pinche_rule);
        tv_pinche_count_rule = (TextView)findViewById(R.id.tv_pinche_count_rule);
        btn_refund = (Button)findViewById(R.id.btn_refund);

        btn_refund.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
        btn_refund.setEnabled(false);
    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        btn_refund.setOnClickListener(this);
        tv_pinche_rule.setOnClickListener(this);
        tv_pinche_count_rule.setOnClickListener(this);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b){
                    btn_refund.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_disable));
                    btn_refund.setEnabled(false);
                }else {
                    btn_refund.setBackgroundDrawable(getResources().getDrawable(R.drawable.commit_order_btn_enable));
                    btn_refund.setEnabled(true);
                }
            }
        });

    }

    @Override
    public void initData() {
        tv_title.setText("退票");
        if (go_type==Constant.LONG){
            tv_refund_style.setText("司机—长途退票");
        }else if (go_type==Constant.CITY){
            tv_refund_style.setText("司机—同城退票");

        }else if (go_type==Constant.TRAVEL){
            tv_refund_style.setText("司机—旅游退票");

        }
        if (order.getIs_pay_loss()==Constant.YES){
            ll_loss_paid.setVisibility(View.VISIBLE);
            ll_loss_not_paid.setVisibility(View.GONE);
        }else {
            ll_loss_paid.setVisibility(View.GONE);
            ll_loss_not_paid.setVisibility(View.VISIBLE);
        }
        tv_phone.setText(order.getPhone());
        if (order.getWho_created()==Constant.PASSENGER){
            tv_host_words.setText("我的留言："+order.getWords());
            tv_host_words.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.chezhu),null,null,null);

        }else {
            tv_host_words.setText("乘客留言："+order.getWords());
            tv_host_words.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.chengke),null,null,null);
        }
        tv_start_pos.setText(order.getStart_place());
        tv_end_pos.setText(order.getEnd_place());
        tv_seats.setText(order.getSeats()+"个");
        tv_total_price.setText(order.getTotal_money()+"元");
        tv_order_code.setText(order.getOrder_code());
        tv_depart_time.setText(order.getDepart_time());
        Picasso.with(this).load(Common.IMAGE_HOST+order.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(iv_icon);
        tv_compensation_money.setText(Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),order.getTotal_money(),0)+"元");
        tv_return_back.setText((Calculator.getdoubleAmount(order.getLoss_money()-Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),order.getTotal_money(),0)))+"");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_pinche_rule:
                Intent iPinRule = new Intent(this, RuleActivity.class);
                iPinRule.putExtra("type", Constant.PINCHE_RULE);
                startActivity(iPinRule);
                break;
            case R.id.tv_pinche_count_rule:
                Intent iPinCountRule = new Intent(this, RuleActivity.class);
                iPinCountRule.putExtra("type",Constant.PINCHE_COUNT_RULE);
                startActivity(iPinCountRule);
                break;
            case R.id.btn_refund:
               /* if (order.getIs_pay_loss()==Constant.YES){
                    Intent intent = new Intent(this, BookPayActivity.class);
                    intent.putExtra("orderCode","");
                    intent.putExtra("amount", "");
                    intent.putExtra("from", "");
                    intent.putExtra("id","");
                    intent.putExtra("alipay", "");
                    intent.putExtra("yivipay","");
                    startActivity(intent);
                }else {
                    showToast("未支付爽约金");
                }*/
                JSONObject object = new JSONObject();
                object.put("role",Constant.HOST+"");
                object.put("orderId",order.getOrder_id());
                object.put("total_amount",order.getTotal_money()+"");
                object.put("quitTicketSeats",order.getSeats()+"");
                object.put("payment",Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),order.getTotal_money(),0)+"");
                object.put("quitAmount",(order.getLoss_money()-Calculator.getPaymentAmount(new Date(order.getLeaveOffTime()),order.getTotal_money(),0))+"");
                httpPost(Common.HOST+Common.REFUND_TICKET,object,Constant.REFUND_TICKET);
                break;
        }
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        Intent intent = new Intent(this, CompleteActivity.class);
        intent.putExtra("type",Constant.REFUND_OK);
        intent.putExtra("text_1",order.getOrder_code());
        intent.putExtra("text_2","["+order.getStart_place()+"]到["+order.getEnd_place()+"]");
        startActivity(intent);

    }
}
